package com.nuliji.help;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import java.util.Collection;

/**
 * Created by GaoJie on 2017/07/31.
 */
@Service
public class ShiroHelp{
    public Subject getSession() {
        return SecurityUtils.getSubject();
    }

    public <T> T getLogin(Class<T> clazz){
        PrincipalCollection pc = SecurityUtils.getSubject().getPrincipals();
        if(pc == null) return null;
        Collection c = pc.byType(clazz);
        return c.size() > 0 ? (T) c.toArray()[0] : null;
    }

    public void logout() {
        Subject subject = getSession();
        subject.logout();
//        RealmSecurityManager sm = (RealmSecurityManager)SecurityUtils.getSecurityManager();
//        Iterator var3 = sm.getRealms().iterator();
//
//        Realm realm;
//        while(var3.hasNext()){
//            realm = (Realm)var3.next();
//        }
    }

    public void refresh(){
        getSession().getSession().touch();
    }
}
